package com.boboPlanet.dict.controller;

import com.alibaba.fastjson.JSON;
import com.boboPlanet.comm.entity.dto.UploadPPTDTO;
import com.boboPlanet.comm.entity.po.FileDetailPO;
import com.boboPlanet.comm.filestorage.PlanetFileBaseTemplate;
import com.boboPlanet.comm.result.ResponseResult;
import com.boboPlanet.dict.service.PlanetFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Description: TODO 星球文件操作API
 * @Author: mis_wu
 * @Date: 2023/7/11
 * @Company planet
 **/
@RestController
@RequestMapping("/file")
@Api(tags = "文件操作",value = "文件操作")
public class PlanetFileController {

    @Resource
    private PlanetFileService planetFileService;


    @PostMapping("/saveFile")
    @ApiOperation("保存文件")
    public ResponseResult<Boolean> saveFile(@RequestBody FileDetailPO fileDetail){
        Boolean saveFile = planetFileService.saveFile(fileDetail);
        if (!saveFile){
            return ResponseResult.error();
        }
        return ResponseResult.isSuccess(true);
    }

    @GetMapping("/getFileByUrl")
    @ApiOperation("根据文件路径获取文件信息")
    public ResponseResult<FileDetailPO> getFileByUrl(@RequestParam String url){
        FileDetailPO file = planetFileService.getFileByUrl(url);
        return ResponseResult.isSuccess(file);
    }

    @GetMapping("/deleteFileById")
    @ApiOperation("根据id删除文件")
    public ResponseResult<Boolean> deleteFileById(@RequestParam Long id){
        return ResponseResult.isSuccess(planetFileService.deleteFileById(id));
    }

    @PostMapping("/updatePPTContent")
    @ApiOperation("修改PPT内容")
    public ResponseResult<Boolean> updatePPTContent(@RequestBody UploadPPTDTO pptDto){
        return ResponseResult.isSuccess(planetFileService.updatePPTContent(pptDto));
    }

    @PostMapping("/updatePPTContentByFile")
    @ApiOperation("修改PPT内容,通过上传原文件方式")
    public ResponseResult<Boolean> updatePPTContentByFile(@RequestParam MultipartFile file,@RequestParam String pptDto){
        UploadPPTDTO uploadPPTDTO = JSON.parseObject(pptDto, UploadPPTDTO.class);
        return ResponseResult.isSuccess(planetFileService.updatePPTContentByFile(file,uploadPPTDTO));
    }

}
